﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace jmwqz
{
    public partial class product_add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string strErr = "";

                if (this.txtproductName.Text.Trim().Length == 0)
                {
                    strErr += "商品名称不能为空！\\n";
                }

                if (!MyCommFun.isNumber(txtstock.Text))
                {
                    strErr += "库存格式错误！\\n";
                }
                if (this.datepicker.Value.ToString().Trim().Length == 0)
                {
                    strErr += "日期不能为空！\\n";
                }
                string strexpiredate = this.datepicker.Value.ToString().Trim();
                DateTime expiredt = DateTime.Now.AddDays(30);
                DateTime.TryParse(strexpiredate, out expiredt);
                if (!string.IsNullOrEmpty(strErr))
                {
                    throw new Exception(strErr);
                }


                bool upselling = true;//上架
                if (radType.SelectedItem.Value == "2")
                {
                    upselling = false;
                }
                int stock = int.Parse(this.txtstock.Text);

                DateTime addDate = DateTime.Now;
                
                decimal jifen = decimal.Parse(this.txtjifen.Text);
                decimal cost = decimal.Parse(this.txtprice.Text);
                string productName = this.txtproductName.Text;
                string shortDesc = this.txtshortDesc.Text;

                #region 保存相册====================
                //检查是否有自定义图片
                string thumb_path = "";
                string original_path = "";
                string[] albumArr = Request.Form.GetValues("hid_photo_name");
                string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
                if (albumArr != null && albumArr.Length > 0)
                {
                    //List<Model.wx_shop_albums> ls = new List<Model.wx_shop_albums>();
                    for (int i = 0; i < albumArr.Length; i++)
                    {
                        string[] imgArr = albumArr[i].Split('|');
                        if (imgArr.Length == 3)
                        {
                            original_path = imgArr[1];
                            thumb_path = imgArr[2];
                            break;
                        }
                    }
                    //model.albums = ls;
                }
                #endregion

                Maticsoft.Model.tb_item model = new Maticsoft.Model.tb_item();
                model.name = productName;
                model.description = shortDesc;
                model.summary = shortDesc;
                model.stock = stock;
                model.price = jifen;
                model.is_sale = upselling ? 1 : 0;
                model.img_thumb = thumb_path;
                model.img_orgin = original_path;
                model.expire_date = expiredt;
                model.cost = cost;
                Maticsoft.BLL.tb_item bll = new Maticsoft.BLL.tb_item();
                if (bll.Add(model))
                {
                    JscriptMsg("***商品录入成功,请继续录入***！", "product_add.aspx", "Success");
                }
                else
                {
                    JscriptMsg("***商品录入失败***！", "", "Success");
                }
            }
            catch(Exception ex)
            {
                JscriptMsg(ex.Message, "", "Fail");
            }

        }


        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="msgcss">CSS样式</param>
        protected void JscriptMsg(string msgtitle, string url, string msgcss)
        {
            string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
    }
}